
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: BooleanField.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.dellroad.stuff.string.ParseContext;

/**
 * Boolean field.
 *
 * @see org.dellroad.sidekar.annotation.SidekarField
 */
public final class BooleanField extends AtomicField<Boolean> {

    BooleanField(String name, Entity<?> entity) {
        super(name, entity, Boolean.class);
    }

    @Override
    public void visit(FieldSwitch fieldSwitch) {
        fieldSwitch.caseBooleanField(this);
    }

    @Override
    public void encode(DataOutputStream output, Boolean value) throws IOException {
        output.writeBoolean(value);
    }

    @Override
    public Boolean decode(DataInputStream input) throws IOException {
        return input.readBoolean();
    }

    @Override
    public String toString(Boolean value) {
        return value.toString();
    }

    @Override
    public Boolean fromString(ParseContext ctx) {
        return Boolean.parseBoolean(ctx.matchPrefix("true|false").group());
    }
}

